package thinkingInJava.generics.wildcard;
// page 390
//: generics/CovariantArrays.java

//class Fruit {}
//class Apple extends Fruit {}
//class Jonathan extends Apple {}
//class Orange extends Fruit {}

public class CovariantArrays {
public static void main(String[] args) {
  Fruit[] fruit = new Apple[10];	// 数组实际类型已经变成 Apple[] 因此只能放 Apple 或它子类
  //Apple ta = new Fruit();	类似这种情况肯定是不行的
  fruit[0] = new Apple(); // OK
  fruit[1] = new Jonathan(); // OK
  Apple[] apples = new Apple[10];
  // Runtime type is Apple[], not Fruit[] or Orange[]:
  try {
    // Compiler allows you to add Fruit:
    fruit[0] = new Fruit(); // ArrayStoreException 编译时不出错是因为 fruit 是声明为 Fruit[] 的，运行时才会异常
    // 想要把这种错误在编译期得到提示,则可以使用泛型.
    //Apple ta = new Fruit();	类似这种情况肯定是不行的
  } catch(Exception e) { System.out.println(e); }
  try {
    // Compiler allows you to add Oranges:
    fruit[0] = new Orange(); // ArrayStoreException
  } catch(Exception e) { System.out.println(e); }
}
} /* Output:
java.lang.ArrayStoreException: Fruit
java.lang.ArrayStoreException: Orange
*///:~

